/*本模块功能:添加专业
        编写者:朱光曜
        编写时间:2017.10.30*/
$(function() {
	function list_tab(msg,num) {
		return '<li class="dd-item" data-id="'+num+'"   siteId="'+msg.siteList[key].siteId+'">' +
			   '<div class="dd-handle" >'+msg.siteList[key].siteName+'</div>' +
			   '</li>';
	}
    var box = new  Alert;
    var loading1 = new Loading();//监控全局ajax添加loading
    loading1.init();
    $(document).ajaxSend(function(){
		loading1.show();

		});
	$(document).ajaxStop(function(){
		loading1.hide();

		});
	var index = {
		init: function() {
			this.bind();
            maxLengthLimit.init("publicText");
		},
		bind: function() {
			var self = this;
			$(".returnBox .looktg").attr("data-departmentid",result.paramData.departmentId);
			$(".professionalDirection .plus").off("click").click(function() { 
				//点击添加方向
				var $_self=$(this);
				var condition=true;
				var num=0;
				$(".professionalDirection .majorDirection").each(function() {
					//遍历专业方向判断是否追加
					if($(this).val()==$(".professionalDirection>div .inputRight:last input").val()&&num!==$(".professionalDirection .majorDirection").length-1){
						condition=false;
					}
					num++;
				});
		
				if($(".professionalDirection .inputRight:last input").val() !== ""&&condition) {
					//追加专业方向
					$(".professionalDirection>div").append('<div class="inputRight"><label for="departName">专业方向:&ensp;</label><input type="text" maxlength="20"  class="publicText majorDirection" placeholder="请输入专业方向"/><img src="pc/images/log_reduce.png" alt="减" class="reduce" /></div>');
					$(".professionalDirection>div").scrollTop($(".professionalDirection>div").height()*99999);
					maxLengthLimit.init("publicText");
				}

			});
			$(document).off("click", ".professionalDirection .reduce").on("click", ".professionalDirection .reduce",function() { 
				//点击删除方向
               
				if($(".professionalDirection>div  .inputRight").length > 1) {//判断可否删除
					$(this).parents(".inputRight").remove();
				}
			});
			self.locationed();
			//加载主页面
			$(".preservation").off("click").click(function(){//点击保存
				self.sub();
			});
			
			
		},
		sub:function(){
			//保存事件
			var self=this;
			var professionName=$("#major").val();
			var directions=[];//方向
			var sites=[];//地点
			var bool=false;//判断是否有特殊字符
			$(".professionalDirection .majorDirection").each(function() {
				//遍历方向
				if($(this).val()!==""){
					if(verification.specialCode($(this).val())!==true) {
						bool=true;
						self.alert1("verification.specialCode($(this).val())");
					}else if($(this).val()=="") {
						return true;

					} else{
						
						directions.push($(this).val());
					}
					
			    }
			
			});
			$("#nestable2 ol li").each(function() {
				//遍历地点
				if($(this).val()!=="") {
					sites.push($(this).attr("siteid"));
			    }
			
			});
			if(professionName==""){
				self.alert1("请输入专业名称");
			}else if(verification.specialCode(professionName)!==true){
				self.alert1("不能输入特殊字符(@/'\"#$%&^*)");
			}else if(directions.length==0){
				self.alert1("请正确输入专业方向");
			}else if(bool){
				self.alert1("不能输入特殊字符(@/'\"#$%&^*)");
			}else if(sites.length==0){
				self.alert1("请选择地点");
			}else{
			
			var parameterList={
				"departmentId":result.paramData.departmentId,
				"professionName":professionName,
				"directions":directions,
				"sites":sites
			};
		
			$.ajax({
				type: "post",
				url: "addProfession",
				async: true,
				data: {
					"jsonStr": JSON.stringify(parameterList)
				},
				success: function(msg) {
					var msgNew = JSON.parse(msg);
					if(msgNew.result=="success"){
						self.alert1("提交成功","refresh");
					
					}else{
						//self.alert1(msgNew.failMsg);
						if(msgNew.failMsg){
							self.alert1(msgNew.failMsg);
						}else{
							self.alert1("提交失败");
						}

					}
				},
				error: function(data) {
					var json = JSON.stringify({
						"pageName": "error"
				    });
					window.location.href = "skipPage?jsonStr=" + json;

				}
			});
			};
		},
		locationed: function() {
			//加载页面事件
			var parameter={
				"departmentId":result.paramData.departmentId
			};
		
			$.ajax({
				type: "post",
				url: "skipAddProfession",
				async: true,
				data: {
					"jsonStr": JSON.stringify(parameter)
				},
				success: function(msg) {
					var msgNew = JSON.parse(msg);
					var locationList = "";
					for(key in msgNew.siteList) {
						locationList += list_tab(msgNew,key);
					}
					$("#nestable .dd-list").html(locationList);
				},
				error: function(data) {
					var json = JSON.stringify({
						"pageName": "error"
				    });
					window.location.href = "skipPage?jsonStr=" + json;

				}
			});

		},
		alert1: function(content1,end) { //confirm提示

			box.contenthead = "提示";
			box.content = content1;
			box.success =function(){
					box.hide();
					if(end){//判断是否需要刷新
						var json = JSON.stringify({
							"pageName": "adminOrganizationalProfessional",
							"paramData": {
								
								"departmentId":result.paramData.departmentId
							}
					    });
						window.location.href = "skipPage?jsonStr=" + json;
					}
				};
				
			

			box.init();
			box.show();
		}

	};
	index.init();
	

	
});